package besteAukera;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.util.Vector;

public class BatugailuKengailu extends Konbinazionalak {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Color kolorea;
	private Vector<Hanka> hankaBektorea = new Vector<Hanka>();
	private Vector<Hanka> sarreraBektorea = new Vector<Hanka>();
	private Hanka s1, s2, i1, i2, s3;

	public BatugailuKengailu(int x1, int y1, int hankaKop, int sarreraKopMax,
			String mota, Color kolorea) {
		super(x1, y1, hankaKop, sarreraKopMax, mota, kolorea);
		// TODO Auto-generated constructor stub
		s1 = new Hanka(x + 20, y - 20, "positiboa", "s", 0, this, "b", "ez",
				false);
		s2 = new Hanka(x + 40, y - 20, "positiboa", "s", 1, this, "b", "ez",
				false);
		s3 = new Hanka(x - 20, y + 20, "positiboa", "s", 2, this, "h", "ez",
				true);
		i1 = new Hanka(x + 30, y + 60, "positiboa", "i", 3, this, "b", "ez",
				false);
		i2 = new Hanka(x + 80, y + 20, "positiboa", "i", 4, this, "h", "ez",
				true);
		lotList[0] = s1;
		lotList[1] = s2;
		lotList[3] = i1;
		lotList[4] = i2;
		lotList[2] = s3;
	}

	public void marraztu(Graphics g) {
		if (getAukeratua() == true) {
			kolorea = Color.GRAY;
		} else {
			kolorea = Color.black;
		}
		g.setColor(kolorea);
		// goikoa
		g.drawLine(x, y, x + 60, y);
		// beikoa
		g.drawLine(x + 60, y, x + 60, y + 40);
		// ezkerretakoa
		g.drawLine(x + 60, y + 40, x, y + 40);
		// eskubitakoa
		g.drawLine(x, y + 40, x, y);
		Font lehenetsia=g.getFont();
		Font f=new Font("name",Font.BOLD,9);
		g.setFont(f);
		if (this.mota.compareTo("bat") == 0) {
			g.drawString("Bat", x + 20, y + 23);
			g.drawString("Cin", x + 1, y + 38);
			g.drawString("Cout", x + 33, y + 38);
		}
		if (this.mota.compareTo("ken") == 0) {
			g.drawString("Ken", x + 20, y + 23);
			g.drawString("Cin", x + 1, y + 38);
			g.drawString("Cout", x + 33, y + 38);
		}
		if (this.mota.compareTo("batken") == 0) {
			g.drawString("Bat/Ken", x + 10, y + 23);
			g.drawString("Cin", x + 1, y + 38);
			g.drawString("Cout", x + 33, y + 38);
		}
		g.setColor(kolorea);
		g.setFont(lehenetsia);
		s1.marraztu(g);
		s2.marraztu(g);
		i1.marraztu(g);
		i2.marraztu(g);
		s3.marraztu(g);
	}

	public boolean barruanDago(int x, int y) {
		if ((x > this.x) && (x < (this.x + 60)) && (y > this.y)
				&& (y < (this.y + 40))) {
			return true;
		}
		return false;
	}

	@Override 
	public void funtzioLogikoa(Integer... values) {

		// TODO Auto-generated method stub
		if (this.mota.compareTo("bat") == 0) {
			// batuketa
			lotList[3].getIrteera().setBalioa(
					s1.getSarrera().getBalioa() + s2.getSarrera().getBalioa()
							+ s3.getSarrera().getBalioa());
			if (getDatuMota().compareTo("Positiboa") == 0) {
				// carry-aren tratamendua
				if (s1.getSarrera().getBalioa() + s2.getSarrera().getBalioa()
						+ s3.getSarrera().getBalioa() > Math.pow(2, bitKop) - 1) {
					lotList[4].getIrteera().setBalioa(1);
				} else
					lotList[4].getIrteera().setBalioa(0);
			} else if (getDatuMota().compareTo("BirakoOs") == 0) {
				System.out
						.println("kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk");
				Balioa b1 = new Balioa(bitKop);
				Balioa b2 = new Balioa(bitKop);
				Balioa bi1 = new Balioa(bitKop);
				Vector<Integer> sar1 = b1.getBalioBitarra(s1.getSarrera()
						.getBalioa());
				Vector<Integer> sar2 = b2.getBalioBitarra(s2.getSarrera()
						.getBalioa());
				Vector<Integer> irt1 = bi1.getBalioBitarra(s1.getSarrera().getBalioa() + s2.getSarrera().getBalioa()
						+ s3.getSarrera().getBalioa());
				if ((sar1.get(bitKop - 1).equals(-sar2.get(bitKop - 1)))) {
					if ((sar2.get(bitKop-1)).equals(-irt1.get(bitKop - 1)))
						lotList[4].getIrteera().setBalioa(0);
					else
						lotList[4].getIrteera().setBalioa(1);

				} else
					lotList[4].getIrteera().setBalioa(0);
			}
		}
		
		// batuketa(s1.getSarrera().getBalioa(),s2.getSarrera().getBalioa(),s3.getSarrera().getBalioa());
	
		if (this.mota.compareTo("ken") == 0) {
			Balioa bal=new Balioa(bitKop);
			int i=0;
			Integer emaitza=0;
			Vector<Integer> b=bal.getBalioBitarra(s1.getSarrera().getBalioa() - (s2.getSarrera().getBalioa()));
			while (i<bitKop){
				emaitza=emaitza+b.get(i)*(int)(Math.pow(2, i));
				i++;
			}
			lotList[3].getIrteera().setBalioa(emaitza);
			if (getDatuMota().compareTo("Positiboa") == 0) {
				// carry-aren tratamendua
				if (s1.getSarrera().getBalioa() + s2.getSarrera().getBalioa()
						+ s3.getSarrera().getBalioa() > Math.pow(2, bitKop) - 1) {
					lotList[4].getIrteera().setBalioa(1);
				} else
					lotList[4].getIrteera().setBalioa(0);
			} else if (getDatuMota().compareTo("BirakoOs") == 0) {
				System.out
						.println("kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk");
				Balioa b1 = new Balioa(bitKop);
				Balioa b2 = new Balioa(bitKop);
				Balioa bi1 = new Balioa(bitKop);
				Vector<Integer> sar1 = b1.getBalioBitarra(s1.getSarrera()
						.getBalioa());
				Vector<Integer> sar2 = b2.getBalioBitarra(s2.getSarrera()
						.getBalioa());
				Vector<Integer> irt1 = bi1.getBalioBitarra(s1.getSarrera().getBalioa() + s2.getSarrera().getBalioa()
						+ s3.getSarrera().getBalioa());
				if ((sar1.get(bitKop - 1).equals(sar2.get(bitKop - 1)))) {
					if (sar2.get(bitKop-1).equals(irt1.get(bitKop - 1)))
						lotList[4].getIrteera().setBalioa(0);
					else
						lotList[4].getIrteera().setBalioa(1);

				} else
					lotList[4].getIrteera().setBalioa(0);
			}
		}
		 if (this.mota.compareTo("batken") == 0) {
		 if (s3.getSarrera().getBalioa() == 0)
		 {

				// batuketa
				lotList[3].getIrteera().setBalioa(
						s1.getSarrera().getBalioa() + s2.getSarrera().getBalioa()
								+ s3.getSarrera().getBalioa());
				if (getDatuMota().compareTo("Positiboa") == 0) {
					// carry-aren tratamendua
					if (s1.getSarrera().getBalioa() + s2.getSarrera().getBalioa()
							+ s3.getSarrera().getBalioa() > Math.pow(2, bitKop) - 1) {
						lotList[4].getIrteera().setBalioa(1);
					} else
						lotList[4].getIrteera().setBalioa(0);
				} else if (getDatuMota().compareTo("BirakoOs") == 0) {
					System.out
							.println("kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk");
					Balioa b1 = new Balioa(bitKop);
					Balioa b2 = new Balioa(bitKop);
					Balioa bi1 = new Balioa(bitKop);
					Vector<Integer> sar1 = b1.getBalioBitarra(s1.getSarrera()
							.getBalioa());
					Vector<Integer> sar2 = b2.getBalioBitarra(s2.getSarrera()
							.getBalioa());
					Vector<Integer> irt1 = bi1.getBalioBitarra(s1.getSarrera().getBalioa() + s2.getSarrera().getBalioa()
							+ s3.getSarrera().getBalioa());
					if ((sar1.get(bitKop - 1).equals(-sar2.get(bitKop - 1)))) {
						if ((sar2.get(bitKop-1)).equals(-irt1.get(bitKop - 1)))
							lotList[4].getIrteera().setBalioa(0);
						else
							lotList[4].getIrteera().setBalioa(1);

					} else
						lotList[4].getIrteera().setBalioa(0);
				}
			
		 }
		 else if (s3.getSarrera().getBalioa() == 1)
		 {

				Balioa bal=new Balioa(bitKop);
				int i=0;
				Integer emaitza=0;
				Vector<Integer> b=bal.getBalioBitarra(s1.getSarrera().getBalioa() - (s2.getSarrera().getBalioa()));
				while (i<bitKop){
					emaitza=emaitza+b.get(i)*(int)(Math.pow(2, i));
					i++;
				}
				lotList[3].getIrteera().setBalioa(emaitza);
				if (getDatuMota().compareTo("Positiboa") == 0) {
					// carry-aren tratamendua
					if (s1.getSarrera().getBalioa() + s2.getSarrera().getBalioa()
							+ s3.getSarrera().getBalioa() > Math.pow(2, bitKop) - 1) {
						lotList[4].getIrteera().setBalioa(1);
					} else
						lotList[4].getIrteera().setBalioa(0);
				} else if (getDatuMota().compareTo("BirakoOs") == 0) {
					System.out
							.println("kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk");
					Balioa b1 = new Balioa(bitKop);
					Balioa b2 = new Balioa(bitKop);
					Balioa bi1 = new Balioa(bitKop);
					Vector<Integer> sar1 = b1.getBalioBitarra(s1.getSarrera()
							.getBalioa());
					Vector<Integer> sar2 = b2.getBalioBitarra(s2.getSarrera()
							.getBalioa());
					Vector<Integer> irt1 = bi1.getBalioBitarra(s1.getSarrera().getBalioa() + s2.getSarrera().getBalioa()
							+ s3.getSarrera().getBalioa());
					if ((sar1.get(bitKop - 1).equals(sar2.get(bitKop - 1)))) {
						if (sar2.get(bitKop-1).equals(irt1.get(bitKop - 1)))
							lotList[4].getIrteera().setBalioa(0);
						else
							lotList[4].getIrteera().setBalioa(1);

					} else
						lotList[4].getIrteera().setBalioa(0);
				}
			
		 }
		 }

	}

	
	@Override
	public Vector<Hanka> getHankak() {
		// TODO Auto-generated method stub
		hankaBektorea = new Vector<Hanka>();
		hankaBektorea.addElement(s1);
		hankaBektorea.addElement(s2);
		hankaBektorea.addElement(s3);
		hankaBektorea.addElement(i1);
		hankaBektorea.addElement(i2);
		return hankaBektorea;
	}

	@Override
	public Vector<Hanka> getSarreraHankak() {
		// TODO Auto-generated method stub
		sarreraBektorea = new Vector<Hanka>();
		sarreraBektorea.addElement(s1);
		sarreraBektorea.addElement(s2);
		sarreraBektorea.addElement(s3);
		return sarreraBektorea;
	}
}
